package org.sanelli.InterstellarGolf.Util;

import org.sanelli.InterstellarGolf.GenericObject.ActivityConstant;
import org.sanelli.InterstellarGolf.GenericObject.BoundingBox;
import org.sanelli.InterstellarGolf.GenericObject.Vector2D;
import org.sanelli.InterstellarGolf.SpecificObject.GameWorld;

import android.util.Log;

public class Scaling {

	private static BoundingBox visibleRegion = new BoundingBox();
	
	public static void calculateScaling(GameWorld world) {
		//Screen ratio
		double sratio = ActivityConstant.screenWidht/ActivityConstant.screenHeight;
		BoundingBox bb = world.getBoundingbox();
		/*World Ratio*/
		double wratio = bb.getRatio();
		
		double p1y = bb.getP1().getY();
		double p1x = bb.getP1().getX();
		double p2y = bb.getP2().getY();
		double p2x = bb.getP2().getX();
		
		if (sratio <= wratio) { //Strech height
			double diff = (bb.diffX() / sratio) - bb.diffY();
			p1y += diff/2;
			p2y -= diff/2;
		}
		else { //Strech width
			double diff = (bb.diffY() * sratio) - bb.diffX();
			p1x -= diff/2;
			p2x += diff/2;
		}
		Scaling.visibleRegion = new BoundingBox(p1x, p1y, p2x, p2y);
		Log.i("Calculate scaling ", Scaling.visibleRegion.toString() );
	}
	
	public static Vector2D WorldToScreen(Vector2D position) {
		double wx = ((position.getX() - Scaling.visibleRegion.getP1().getX()) / (Scaling.visibleRegion.diffX())*ActivityConstant.screenWidht );
		double wy = ActivityConstant.screenHeight - (position.getY() - Scaling.visibleRegion.getP2().getY()) / (Scaling.visibleRegion.diffY())*ActivityConstant.screenHeight;
		return new Vector2D(wx, wy);
	}
	
	public static Vector2D WorldToScreen(float x, float y) {
		double wx = ((x - Scaling.visibleRegion.getP1().getX()) / (Scaling.visibleRegion.diffX())*ActivityConstant.screenWidht );
		double wy = ActivityConstant.screenHeight - ((y - Scaling.visibleRegion.getP1().getY()) / (Scaling.visibleRegion.diffY())*ActivityConstant.screenHeight );
		return new Vector2D(wx, wy);
	}
		
	public static Vector2D ScreenToWorld(Vector2D position) {
		double wx = position.getX() * Scaling.visibleRegion.diffX() / ActivityConstant.screenWidht + Scaling.visibleRegion.getP1().getX();
		double wy = (ActivityConstant.screenHeight - position.getY()) * Scaling.visibleRegion.diffY() / ActivityConstant.screenHeight + Scaling.visibleRegion.getP2().getY();
		return new Vector2D(wx, wy);
	}
	
	public static Vector2D ScreenToWorld(float f, float g) {
		double wx = f * Scaling.visibleRegion.diffX() / ActivityConstant.screenWidht + Scaling.visibleRegion.getP1().getX();
		double wy = (ActivityConstant.screenHeight - g) * Scaling.visibleRegion.diffY() / ActivityConstant.screenHeight + Scaling.visibleRegion.getP2().getY();
		return new Vector2D(wx, wy);
	}
	
}
